﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Speech.Synthesis;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Xml.Linq;
using WpfApp1.bean;
using WpfApp1.CMD;
using WpfApp1.Httpwork;
using WpfApp1.Main;

/**主页面，主要操作页面*/
namespace WpfApp1
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    /// 
    public partial class Window1 : Window, MainInterface
    {
        string shuruguolv = "";
        String scanCode = "0";

        /**扫描枪串口*/
        SerialPort serialPortPan;

        ArrayList pan = new ArrayList();
        List<String> batchs = new List<String>();
        //ArrayList packages = new ArrayList();
        public static Dictionary<String, packagebean> packages = new Dictionary<String, packagebean>();
        public static Dictionary<String, panbean> panonum = new Dictionary<String, panbean>();
        ArrayList chuankous = new ArrayList();
        //List<String> categorys = new List<String>();
        myCom mycom;
        MainM M;
        String AP = "";
        int rate = 0;
        String[] buffers = { "602001", "602002", "602003", "602004", "602005", "602006", "602007", "602008", "602009", "602010", "602011", "602012" };
        public Window1()
        {
            InitializeComponent();
            Loaded += InitWindowActualHeight_OnLoaded;
            Console.WriteLine(buffers.Length);
            readBuffer();
            mycom = new myCom(this);
            M = new MainM(this, batchs, packages, panonum, chuankous,buffers,mycom);
            M.setAP(AP);
            M.readbaoXXL();
            M.HttpHuoquJiehao();
            //InitCOM();
        }

        /**获取设置好的缓冲仓位*/
        public void readBuffer()
        {
            
            string filePath = MainWindow.path + "save\\buffer_setting.xml";

            if (File.Exists(filePath))
            {
                XElement xmlDoc = XElement.Load(filePath);
                String buffer = xmlDoc.Attribute("buffer_postions").Value;
                buffers = buffer.Split(new[] { "," }, StringSplitOptions.None);
               
            }

            string filePatap = MainWindow.path + "save\\ap_setting.xml";

            if (File.Exists(filePatap))
            {
                XElement xmlDoc = XElement.Load(filePatap);
                AP = xmlDoc.Attribute("bufferap").Value;
            }

            string filerate = MainWindow.path + "save\\rate_setting.xml";

            if (File.Exists(filerate))
            {
                XElement xmlDoc = XElement.Load(filerate);
                rate = int.Parse(xmlDoc.Attribute("ratey").Value);
            }
        }

        private void connectChuanKou()
        {
            //Console.WriteLine("================chuankous=" + chuankous.Count);
            //foreach (chuankoubean bean in chuankous)
            //{
            //    if (bean.chuankouhao != "")
            //    {
            //        InitCOM(bean); 
            //    }
            //}
        }

        //刷新数据，将数据显示出来
        public void initdata()
        {

            pan0.Items.Clear();
            pan1.Items.Clear();
            pan2.Items.Clear();
            pan3.Items.Clear();
            pan4.Items.Clear();
            pan5.Items.Clear();
            pan6.Items.Clear();
            pan7.Items.Clear();
            pan8.Items.Clear();
            pan9.Items.Clear();
            pan10.Items.Clear();
            pan11.Items.Clear();
            pan12.Items.Clear();
            foreach (KeyValuePair<String, panbean> p in panonum)
            {
                p.Value.onum = "";
            }
           
            foreach (KeyValuePair<String, packagebean> p in packages)
            {
                

                packagebean bean = p.Value;
                
                if (bean.pallet_id != "0")
                {
                    panonum[bean.pallet_id].onum = bean.onum;                                                                 
                }
                if (bean.pallet_id == "0")
                {
                    if (bean.deliver_time != "" || bean.pallet_num != "")
                    {
                        continue;
                    }
                    if (bean.pcode.Contains(shuruguolv))
                    {
                        pan0.Items.Add(bean);
                    }
                }else if (bean.pallet_id ==  buffers[0])
                {
                    pan1.Items.Add(bean);
                }else if (bean.pallet_id == buffers[1])
                {
                    pan2.Items.Add(bean);
                }else if (bean.pallet_id == buffers[2])
                {
                    pan3.Items.Add(bean);
                }else if (bean.pallet_id == buffers[3])
                {
                    pan4.Items.Add(bean);
                }else if (bean.pallet_id == buffers[4])
                {
                    pan5.Items.Add(bean);
                }else if (bean.pallet_id == buffers[5])
                {
                    pan6.Items.Add(bean);
                }else if (bean.pallet_id == buffers[6])
                {
                    pan7.Items.Add(bean);
                }else if (bean.pallet_id == buffers[7])
                {
                    pan8.Items.Add(bean);
                }else if (bean.pallet_id == buffers[8])
                {
                    pan9.Items.Add(bean);
                }else if (bean.pallet_id == buffers[9])
                {
                    pan10.Items.Add(bean);
                }else if (bean.pallet_id == buffers[10])
                {
                    pan11.Items.Add(bean);
                }else if (bean.pallet_id == buffers[11])
                {
                    pan12.Items.Add(bean);
                }
            }
            pan1.Items.Refresh();
            pan2.Items.Refresh();
            pan3.Items.Refresh();
            pan4.Items.Refresh();
            pan5.Items.Refresh();
            pan6.Items.Refresh();
            pan7.Items.Refresh();
            pan8.Items.Refresh();
            pan9.Items.Refresh();
            pan10.Items.Refresh();
            pan11.Items.Refresh();
            pan12.Items.Refresh();

            

            shuliang1.Content = pan1.Items.Count + "包";
            shuliang2.Content = pan2.Items.Count + "包";
            shuliang3.Content = pan3.Items.Count + "包";
            shuliang4.Content = pan4.Items.Count + "包";
            shuliang5.Content = pan5.Items.Count + "包";
            shuliang6.Content = pan6.Items.Count + "包";
            shuliang7.Content = pan7.Items.Count + "包";
            shuliang8.Content = pan8.Items.Count + "包";
            shuliang9.Content = pan9.Items.Count + "包";
            shuliang10.Content = pan10.Items.Count + "包";
            shuliang11.Content = pan11.Items.Count + "包";
            shuliang12.Content = pan12.Items.Count + "包";

            var batch_name = "";
            foreach (String b in batchs)
            {
                batch_name = batch_name + b + " ";
            }
            pan10.Items.Refresh();
            batch.Text = batch_name;
            foreach (KeyValuePair<String, panbean> p in panonum)
            {
                panbean b = p.Value;
                if (p.Key == buffers[0])
                {
                    pallet1.Text = b.pallet;
                    onum1.Text = b.onum;
                    zongShuliang1.Content = "剩"+onumPsize(b.onum)+"包";
                }
                else if (p.Key == buffers[1])
                {
                    pallet2.Text = b.pallet;
                    onum2.Text = b.onum;
                    zongShuliang2.Content = "剩" + onumPsize(b.onum) + "包";
                }
                else if (p.Key == buffers[2])
                {
                    pallet3.Text = b.pallet;
                    onum3.Text = b.onum;
                    zongShuliang3.Content = "剩" + onumPsize(b.onum) + "包";
                }
                else if (p.Key == buffers[3])
                {
                    pallet4.Text = b.pallet;
                    onum4.Text = b.onum;
                    zongShuliang4.Content = "剩" + onumPsize(b.onum) + "包";
                }
                else if (p.Key == buffers[4])
                {
                    pallet5.Text = b.pallet;
                    onum5.Text = b.onum;
                    zongShuliang5.Content = "剩" + onumPsize(b.onum) + "包";
                }
                else if (p.Key == buffers[5])
                {
                    pallet6.Text = b.pallet;
                    onum6.Text = b.onum;
                    zongShuliang6.Content = "剩" + onumPsize(b.onum) + "包";
                }
                else if (p.Key == buffers[6])
                {
                    pallet7.Text = b.pallet;
                    onum7.Text = b.onum;
                    zongShuliang7.Content = "剩" + onumPsize(b.onum) + "包";
                }
                else if (p.Key == buffers[7])
                {
                    pallet8.Text = b.pallet;
                    onum8.Text = b.onum;
                    zongShuliang8.Content = "剩" + onumPsize(b.onum) + "包";
                }
                else if (p.Key == buffers[8])
                {
                    pallet9.Text = b.pallet;
                    onum9.Text = b.onum;
                    zongShuliang9.Content = "剩" + onumPsize(b.onum) + "包";
                }
                else if (p.Key == buffers[9])
                {
                    pallet10.Text = b.pallet;
                    onum10.Text = b.onum;
                    zongShuliang10.Content = "剩" + onumPsize(b.onum) + "包";
                }
                else if (p.Key == buffers[10])
                {
                    pallet11.Text = b.pallet;
                    onum11.Text = b.onum;
                    zongShuliang11.Content = "剩" + onumPsize(b.onum) + "包";
                }
                else if (p.Key == buffers[11])
                {
                    pallet12.Text = b.pallet;
                    onum12.Text = b.onum;
                    zongShuliang12.Content = "剩" + onumPsize(b.onum) + "包";
                }
            }
        }

        //获取该单未扫共多少包
        private int onumPsize(String onum)
        {
            int size = 0;
            foreach (KeyValuePair<String, packagebean> p in packages)
            {
                packagebean bean = p.Value;
                if(bean.onum == onum && bean.pallet_id == "0")
                {

                    if (bean.deliver_time != "" || bean.pallet_num != "")
                    {
                        continue;
                    }
                    size++;
                }
            }
            return size;
        }


        //点击添加节号
        private void OnButtonClick_add_batchi(object sender, RoutedEventArgs e)
        {
            //M.clean();
            //mycom.quenchLight(int.Parse(addbatch.Text));
            M.addJiehao(addbatch.Text);
        }

        //点击删除节号
        private void OnButtonClick_delete_batch(object sender, RoutedEventArgs e)
        {
            //mycom.RedLight(int.Parse(addbatch.Text));
            M.removeJiehao(addbatch.Text);
        }

        //将包号从这个盘分配 
        private void Button_YiChu(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;

            var bean = btn.DataContext as packagebean;

            if (bean.pallet_id == "0")
            {
                M.writerizhi(bean.pcode, "点击缓冲区组盘");
                if (MessageBox.Show("是否将这个包移动到AP盘（选否移动到TP盘）", "提示", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
                {
                    M.setAP(bean);
                }
                else
                {
                    M.shuaixuanAP(bean, false);
                }
                
            }
            else
            {
                M.writerizhi(bean.pcode, "点击移出组盘");
                M.yichu_zupan(bean);
            }
            
        }

        private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void OnButtonClick_ok_zupan1(object sender, RoutedEventArgs e)
        {
            M.postpan(buffers[0]);
        }

        private void OnButtonClick_ok_zupan2(object sender, RoutedEventArgs e)
        {
            M.postpan(buffers[1]);
        }

        private void OnButtonClick_ok_zupan3(object sender, RoutedEventArgs e)
        {
            M.postpan(buffers[2]);
        }

        private void OnButtonClick_ok_zupan4(object sender, RoutedEventArgs e)
        {
            M.postpan(buffers[3]);
        }

        private void OnButtonClick_ok_zupan5(object sender, RoutedEventArgs e)
        {
            M.postpan(buffers[4]);
        }

        private void OnButtonClick_ok_zupan6(object sender, RoutedEventArgs e)
        {
            M.postpan(buffers[5]);
        }

        private void OnButtonClick_ok_zupan7(object sender, RoutedEventArgs e)
        {
            M.postpan(buffers[6]);
        }

        private void OnButtonClick_ok_zupan8(object sender, RoutedEventArgs e)
        {
            M.postpan(buffers[7]);
        }

        private void OnButtonClick_ok_zupan9(object sender, RoutedEventArgs e)
        {
            M.postpan(buffers[8]);
        }

        private void OnButtonClick_ok_zupan10(object sender, RoutedEventArgs e)
        {
            M.postpan(buffers[9]);
        }

        private void OnButtonClick_ok_zupan11(object sender, RoutedEventArgs e)
        {
            M.postpan(buffers[10]);
        }

        private void OnButtonClick_ok_zupan12(object sender, RoutedEventArgs e)
        {
            M.postpan(buffers[11]);
        }

        private void OnButtonClick_add_tuopan1(object sender, RoutedEventArgs e)
        {
            scanCode = buffers[0];
            cleanOperation("扫描托盘号为托盘一添加托盘号",false);
            mycom.BangpanLight(1);
        }

        private void OnButtonClick_add_tuopan2(object sender, RoutedEventArgs e)
        {
            scanCode = buffers[1];
            cleanOperation("扫描托盘号为托盘二添加托盘号",false);
            mycom.BangpanLight(2);
        }

        private void OnButtonClick_add_tuopan3(object sender, RoutedEventArgs e)
        {
            scanCode = buffers[2];
            cleanOperation("扫描托盘号为托盘三添加托盘号",false);
            mycom.BangpanLight(3);
        }

        private void OnButtonClick_add_tuopan4(object sender, RoutedEventArgs e)
        {
            scanCode = buffers[3];
            cleanOperation("扫描托盘号为托盘四添加托盘号",false);
            mycom.BangpanLight(4);
        }

        private void OnButtonClick_add_tuopan5(object sender, RoutedEventArgs e)
        {
            scanCode = buffers[4];
            cleanOperation("扫描托盘号为托盘五添加托盘号",false);
            mycom.BangpanLight(5);
        }

        private void OnButtonClick_add_tuopan6(object sender, RoutedEventArgs e)
        {
            scanCode = buffers[5];
            cleanOperation("扫描托盘号为托盘六添加托盘号",false);
            mycom.BangpanLight(6);
        }

        private void OnButtonClick_add_tuopan7(object sender, RoutedEventArgs e)
        {
            scanCode = buffers[6];
            cleanOperation("扫描托盘号为托盘七添加托盘号",false);
            mycom.BangpanLight(7);
        }

        private void OnButtonClick_add_tuopan8(object sender, RoutedEventArgs e)
        {
            scanCode = buffers[7];
            cleanOperation("扫描托盘号为托盘八添加托盘号",false);
            mycom.BangpanLight(8);
        }

        private void OnButtonClick_add_tuopan9(object sender, RoutedEventArgs e)
        {
            scanCode = buffers[8];
            cleanOperation("扫描托盘号为托盘九添加托盘号",false);
            mycom.BangpanLight(9);
        }

        private void OnButtonClick_add_tuopan10(object sender, RoutedEventArgs e)
        {
            scanCode = buffers[9];
            cleanOperation("扫描托盘号为托盘十添加托盘号",false);
            mycom.BangpanLight(10);
        }

        private void OnButtonClick_add_tuopan11(object sender, RoutedEventArgs e)
        {
            scanCode = buffers[10];
            cleanOperation("扫描托盘号为托盘十一添加托盘号",false);
            mycom.BangpanLight(11);
        }

        private void OnButtonClick_add_tuopan12(object sender, RoutedEventArgs e)
        {
            scanCode = buffers[11];
            cleanOperation("扫描托盘号为托盘十二添加托盘号",false);
            mycom.BangpanLight(12);
        }

       

        //打开串口的方法
        public bool OpenPort(SerialPort serialPort)
        {
            try//这里写成异常处理的形式以免串口打不开程序崩溃
            {
                serialPort.Open();
            }
            catch { }
            if (serialPort.IsOpen)
            {
                MessageBox.Show("串口打开成功!");
                return true;
            }
            else
            {
                MessageBox.Show("串口打开失败!");
                return false;
            }
        }
        ////向串口发送数据
        //public void SendCommand(string CommandString)
        //{
        //    byte[] WriteBuffer = Encoding.ASCII.GetBytes(CommandString);
        //    serialPort.Write(WriteBuffer, 0, WriteBuffer.Length);
        //}

        /// 盘1扫描口数据接收事件
        private void serialPort_pan1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Thread.Sleep(200);
            var s = serialPortPan.ReadExisting();
            var bean = new panbean();
            Dispatcher.BeginInvoke(new Action(delegate
              {
                  Scan.Text = s.Trim();
                  Thread.Sleep(200);
                  scanData(s.Trim());
              }));
        }

        private void OnButtonClick_scan(object sender, RoutedEventArgs e)
        {
            scanData(Scan.Text);
        }
        private void Deng1(object sender, RoutedEventArgs e)
        {
            mycom.greenLight(int.Parse(Scan.Text));
        }
        private void Deng2(object sender, RoutedEventArgs e)
        {
            mycom.mieLight(int.Parse(Scan.Text));
        }
        private void Deng3(object sender, RoutedEventArgs e)
        {
            mycom.redLight(int.Parse(Scan.Text));
        }
        /**处理扫描到的内容*/
        public void scanData(string datao)
        {

            string[] datas = datao.Split('#');
            string data = datas[datas.Length - 1];
            M.writerizhi(data, "扫描到的数据"+datao);
            foreach (string s in buffers)  
            {
                if (data == s)
                {
                    if (panonum[s].onum == "")
                    {
                        ArrayList arr = new ArrayList(buffers);

                        cleanOperation("扫描托盘号为位置" + (arr.IndexOf(s) + 1) + "添加托盘号",false);
                        scanCode = s;
                        mycom.BangpanLight((arr.IndexOf(s) + 1));
                        M.writerizhi(data, "扫描的是仓位号，下一步为仓位号添加托盘");
                        return;
                    }
                    else
                    {
                        cleanOperation(data + "正在组盘，不能添加托盘",false);
                        M.writerizhi(data, "扫描的是仓位号，正在组盘，不能添加托盘");
                        return;
                        //MessageBox.Show(data+"正在组盘，不能添加托盘");
                    }

                }
            }
            
            if (scanCode != "0")
            {
                foreach (string s in buffers)
                {
                    
                    if (scanCode == s)
                    {
                        if (data.Substring(0, 2) == "TP" && data.Length == 8 && AP != s)
                        {
                            var bean = panonum[s];
                            bean.pallet = data;
                            panonum[s] = bean;
                            cleanOperation("添加托盘" + data + "到位置" + s,false);
                            M.writerizhi(data, "扫描的是托盘号，将托盘添加到"+s);
                            ArrayList arr = new ArrayList(buffers);
                            var pan = arr.IndexOf(s) + 1;
                            mycom.quenchLight(pan);
                            M.getPalletdata(data,pan);
                            M.addYiYou();
                            cleanView();
                            M.writsqlpan(panonum[s]);
                            //M.writSqlPackages();
                            
                            return;
                        }
                        if (data.Substring(0, 2) == "AP" && AP == s)
                        {
                            var bean = panonum[s];
                            bean.pallet = data;
                            panonum[s] = bean;
                            cleanOperation("添加托盘" + data + "到位置" + s,false);
                            M.writerizhi(data, "扫描的是托盘号，将托盘添加到" + s);
                            ArrayList arr = new ArrayList(buffers);
                            mycom.quenchLight(arr.IndexOf(s) + 1);
                            M.addYiYou();
                            cleanView();
                            M.writsqlpan(panonum[s]);
                            //M.writSqlPackages();

                            return;
                        }
                    }

                    
                }
            }
            alter_backgrounf(-1);
            mycom.miehong();
            if (data.Length > 13 && data.Length < 30)
            {


                if (packages.ContainsKey(data))
                {

                    var package = packages[data];
                    M.shuaixuanAP(package,true);
                    scanCode = "0";
                    return;
                }
                else
                {
                    M.addPcodeBatch(data);
                    cleanOperation("没有找到包条码" + data + "请录入该节号后在扫描条码",false);
                    M.writerizhi(data, "扫描的是包条码，没找到，网络访问获取节号数据");
                    //MessageBox.Show("没有找到包条码" + data + "请录入该节号后在扫描条码");
                    scanCode = "0";
                    return;
                }
            }
            cleanOperation("扫描到的东西不符合各种情况", false);
            M.writerizhi(data, "扫描到的东西不符合各种情况");

        }
        /**扫描仓位或者点击添加托盘后处理*/
        public void scanBuffer()
        {
            cleanOperation("扫描托盘为仓位"+ scanCode+"添加托盘",false);
        }


        //刷新串口数据
        public void cleanChuankou()
        {
            if(serialPortPan != null)
            {
                serialPortPan.Close();
            }

            connectChuanKou();
        }
        //刷新界面数据
        public void cleanView()
        {
            Dispatcher.BeginInvoke(new Action(delegate
            {
                initdata();
            }));
            
        }

        private void Button_shezhi(object sender, RoutedEventArgs e)
        {
            Setting Mn = new Setting(chuankous);
            Mn.getTextHandler = GetValue;
            Mn.ShowDialog();
        }

        private void Button_chaxun(object sender, RoutedEventArgs e)
        {
            ChaXunWindow Mn = new ChaXunWindow();
            Mn.Show();
        }

        private void Button_jilu(object sender, RoutedEventArgs e)
        {
            ScanRecordLog Mn = new ScanRecordLog();
            Mn.Show();
        }


        public void GetValue()
        {
            M.cleanCom();
            readBuffer();
            M.setAP(AP);
            M.setRate(rate);
            M.buffers = buffers;
            M.cleanbuffer();
            M.buffers = buffers;
            M.HttpHuoquJiehao();
        }

        public delegate void D_Math();
        //刷新提示文本
        public void cleanOperation(string s,Boolean isred)
        {
            this.Dispatcher.Invoke(new Action(delegate
            {
                if (isred)
                {
                    Operation.Background = new SolidColorBrush(Colors.Red);
                }
                else
                {
                    Operation.Background = new SolidColorBrush(Colors.BlanchedAlmond);
                }
                Dispatcher.BeginInvoke(new Action(delegate
                {
                    Operation.Text = s;
                }));
            }));
            
        }
        /**扫描枪扫描到的数据*/
        public void scrm_com(string scrm)
        {
            Dispatcher.BeginInvoke(new Action(delegate
            {
                Scan.Text = scrm.Trim();
                scanData(scrm.Trim());
            }));
        }

        public void alter_backgrounf(int pan)
        {
            alter_color(1, pan, pan1);
            alter_color(2, pan, pan2);
            alter_color(3, pan, pan3);
            alter_color(4, pan, pan4);
            alter_color(5, pan, pan5);
            alter_color(6, pan, pan6);
            alter_color(7, pan, pan7);
            alter_color(8, pan, pan8);
            alter_color(9, pan, pan9);
            alter_color(10, pan, pan10);
            alter_color(11, pan, pan11);
            alter_color(12, pan, pan12);
        }

        public void alter_color(int wei,int pan, ListView panname)
        {

            if (wei == pan)
            {

                Thread t = new Thread(new ThreadStart(() =>
                {
                    Thread.Sleep(300);
                    Dispatcher.BeginInvoke(new Action(delegate
                    {
                        panname.Background = new SolidColorBrush(Colors.Red);
                    }));
                }));
                t.Start();
            }
            else
            {
                Dispatcher.BeginInvoke(new Action(delegate
                {
                    panname.Background = new SolidColorBrush(Colors.White);
                }));
                
            }
        }

        public void sleep300(ListView panname)
        {
            Thread.Sleep(300);
            Dispatcher.BeginInvoke(new Action(delegate
            {

                panname.Background = new SolidColorBrush(Colors.Red);
            }));
        }

        public void zpbt_backgrounf(int pan,Boolean ishong)
        {
            switch (pan)
            {
                case 1:
                    zpbt_color(tuo1,ishong);
                    break;
                case 2:
                    zpbt_color(tuo2, ishong);
                    break;
                case 3:
                    zpbt_color(tuo3, ishong);
                    break;
                case 4:
                    zpbt_color(tuo4, ishong);
                    break;
                case 5:
                    zpbt_color(tuo5, ishong);
                    break;
                case 6:
                    zpbt_color(tuo6, ishong);
                    break;
                case 7:
                    zpbt_color(tuo7, ishong);
                    break;
                case 8:
                    zpbt_color(tuo8, ishong);
                    break;
                case 9:
                    zpbt_color(tuo9, ishong);
                    break;
                case 10:
                    zpbt_color(tuo10, ishong);
                    break;
                case 11:
                    zpbt_color(tuo11, ishong);
                    break;
                case 12:
                    zpbt_color(tuo12, ishong);
                    break;
            }

        }

        public void zpbt_color(Button tuobtname, Boolean ishong)
        {
            if (ishong)
            {
                Dispatcher.BeginInvoke(new Action(delegate
                {
                    tuobtname.Background = new SolidColorBrush(Colors.Red);
                }));
            }
            else
            {
                Dispatcher.BeginInvoke(new Action(delegate
                {
                    tuobtname.Background = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#DDDDDD"));
                }));
            }
        }

        //待组盘区输入时刷新待组盘区
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            shuruguolv = ZuPanGuoLv.Text;
            pan0.Items.Clear();
            foreach (KeyValuePair<String, packagebean> p in packages)
            {

                packagebean bean = p.Value;
                if (bean.pallet_id == "0")
                {
                    if (bean.deliver_time != "" || bean.pallet_num !=  "")
                    {
                        continue;
                    }
                    if (bean.pcode.Contains(shuruguolv))
                    {
                        pan0.Items.Add(bean);
                    }
                }
            }
            pan0.Items.Refresh();
        }

        #region 设置窗口对屏幕高度的自适应
 
         private void InitWindowActualHeight_OnLoaded(object sender, RoutedEventArgs e)
         {
             Loaded -= InitWindowActualHeight_OnLoaded;
             InitWindowActualHeight();
         }  
 
         private const double WindowShadowHeight = 50;
 
         private void InitWindowActualHeight()
         {
             //获取窗体所在屏幕的高度
             var visibleAreaHeight = GetScreenHeight();
 
                 //设置Viewbox高度=可视高度-阴影高度（此处通过绽放显示窗口，所以不能通过设置窗口或者设置内容的高度来实现）
                 RootViewbox.Height = visibleAreaHeight - WindowShadowHeight;
                 //等比例缩小
                 RootViewbox.Stretch = Stretch.Uniform;
         }
         const double DpiPercent = 96;
         private double GetScreenHeight()
         {
             var intPtr = new WindowInteropHelper(this).Handle;//获取当前窗口的句柄
             var screen = System.Windows.Forms.Screen.FromHandle(intPtr);//获取当前屏幕
 
             double height = 0;
             using (Graphics currentGraphics = Graphics.FromHwnd(intPtr))
             {
                 double dpiXRatio = currentGraphics.DpiX / DpiPercent;
                 double dpiYRatio = currentGraphics.DpiY / DpiPercent;
                 height = screen.WorkingArea.Height / dpiYRatio;
                 //var width = screen.WorkingArea.Width / dpiXRatio;
                 //var left = screen.WorkingArea.Left / dpiXRatio;
                 //var top = screen.WorkingArea.Top / dpiYRatio;
             }
             return height;
         }


    }
    class data
    {
        public List<String> batchs { get; set; }
        public List<String> categorys { get; set; }
        public string pcode { get; set; }
        public string pallet_num { get; set; }
    }
}
#endregion